<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id.toString()">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px">{{
              item.pubdate | relativeTime
            }}</span>
            <van-button size="mini" type="default" @click="hClickReply(item)">回复</van-button>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
          @click="hSwitchCommentLike(item)"
        />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容" v-model.trim="content">
        <van-button slot="button" size="mini" type="info" @click="hAddComment"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->

    <!-- 评论回复 -->
    <van-popup
      v-model="isReplyShow"
      round
      position="bottom"
      :style="{ height: '85%' }"
      v-if="isReplyShow"
    >
      <comment-reply :comment="currentComment" :articleId="articleId" @close="isReplyShow = false"></comment-reply>
    </van-popup>
    <!-- 评论回复 -->
  </div>
</template>

<script>
import {
  addComment,
  getComment,
  addCommentLike,
  deleteCommentLike
} from '@/api/comment.js'
import CommentReply from './commentReply.vue'
export default {
  name: 'ArticleComment',
  components: {
    CommentReply
  },
  props: {
    articleId: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      content: '', // 评论内容
      offset: null, // 获取评论数据的偏移量，值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数据
      isReplyShow: false, // 是否显示评论回复页面
      currentComment: {} // 存放要回复的评论的信息
    }
  },

  methods: {
    // 获取文章评论
    async onLoad () {
      try {
        // 1.发送请求
        const res = await getComment(this.articleId, this.offset)
        // console.log('获取文章评论', res)
        // 2.获取数据
        const arr = res.data.data.results
        this.list.push(...arr)
        // 3.手动取消loading
        this.loading = false
        // 4.判断是否加载完成
        this.finished = !arr.length
        // 5.更新offset
        this.offset = res.data.data.last_id
      } catch (err) {
        this.$toast.fail('获取评论失败')
        this.loading = false
      }
    },
    // 添加评论
    async hAddComment () {
      if (this.content === '') {
        return
      }
      try {
        // 1.发送请求
        const res = await addComment(this.articleId, this.content)
        console.log(res)
        // 2.页面渲染
        this.list.unshift(res.data.data.new_obj)
        // 3.清空输入内容
        this.content = ''
        this.$toast.success('发布成功')
      } catch (err) {
        this.$toast.fail('发布失败')
      }
    },
    // 对评论/回复点赞 或者取消点赞
    async hSwitchCommentLike (comment) {
      try {
        const commentId = comment.com_id.toString()
        if (comment.is_liking) {
          // 取消点赞
          await deleteCommentLike(commentId)
          // console.log('取消', res)
          this.$toast.success('取消点赞')
        } else {
          // 进行点赞
          await addCommentLike(commentId)
          // console.log('成功', res)
          this.$toast.success('点赞成功')
        }
        // 渲染页面 因为是true和false 直接进行取反
        comment.is_liking = !comment.is_liking
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    },
    // 控制评论回复弹窗是否显示
    hClickReply (item) {
      // 1.获取要回复评论的信息
      this.currentComment = item
      // 2.显示弹窗
      this.isReplyShow = true
    }
  }
}
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
